package com.cloud.pole.common.interceptor;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.context.BaseContextHandler;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
/**
 * @author ace
 */
@Component
public class OkHttpTokenInterceptor implements Interceptor {

    private Logger logger = LoggerFactory.getLogger(OkHttpTokenInterceptor.class);

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request newRequest = null;
        if (chain.request().url().toString().contains("client/token")) {
        	logger.debug("*************************************************【");
        	logger.debug("客户端获取响应");
        	logger.debug(CommonConstants.AUTH_USER_TOKEN_HEADER + "-" + BaseContextHandler.getToken());
        	logger.debug("requestUrl-" + chain.request().url().toString());

        	logger.debug("*************************************************】");
            newRequest = chain.request()
                    .newBuilder()
                    .header(CommonConstants.AUTH_USER_TOKEN_HEADER, BaseContextHandler.getToken())
                    .build();
        } else {
        	String clientToken = chain.request().header(CommonConstants.AUTH_CLIENT_TOKEN_HEADER);
        	if(StringUtils.isEmpty(clientToken)) {
              	logger.error("获得头head token 为空！");
            	JWTInfo jwtInfo = new JWTInfo(CommonConstants.CLIENT_USER_NAME, JWTInfo.USER_TYPE_CLINET, CommonConstants.CLIENT_USER_USER_ID, CommonConstants.CLIENT_NICK_NAME, CommonConstants.CLIENT_REAL_NAME);
        		clientToken = JwtTokenUtil.generateToken(jwtInfo, CommonConstants.CLIENT_TOKEN_EXPIRE);
        	}
        	
          	logger.debug("*************************************************【");
        	logger.debug("客户端获取响应");
        	logger.debug(CommonConstants.AUTH_USER_TOKEN_HEADER+ "-" + BaseContextHandler.getToken());
        	logger.debug(CommonConstants.AUTH_CLIENT_TOKEN_HEADER+ "-" + clientToken);

        	logger.debug("requestUrl-" + chain.request().url().toString());
        	logger.debug("*************************************************】");
        	newRequest = chain.request()
                    .newBuilder()
                    .header(CommonConstants.AUTH_USER_TOKEN_HEADER, BaseContextHandler.getToken())
                    .header(CommonConstants.AUTH_CLIENT_TOKEN_HEADER, clientToken)
                    .build();
        }
        Response response = chain.proceed(newRequest);
        if (HttpStatus.FORBIDDEN.value() == response.code()) {
            if (response.body().string().contains(String.valueOf(CommonConstants.TOKEN_FORBID_CODE))) {
            	logger.info("Client Token Expire,Retry to request...");
            	JWTInfo jwtInfo = new JWTInfo(CommonConstants.CLIENT_USER_NAME, JWTInfo.USER_TYPE_CLINET, CommonConstants.CLIENT_USER_USER_ID, CommonConstants.CLIENT_NICK_NAME, CommonConstants.CLIENT_REAL_NAME);
            	String clientToken = JwtTokenUtil.generateToken(jwtInfo, CommonConstants.CLIENT_TOKEN_EXPIRE);
            	newRequest = chain.request()
                        .newBuilder()
                        .header(CommonConstants.AUTH_USER_TOKEN_HEADER, BaseContextHandler.getToken())
                        .header(CommonConstants.AUTH_CLIENT_TOKEN_HEADER, clientToken)
                        .build();
                response = chain.proceed(newRequest);
            }
        }
        return response;
    }
}
